package com.fingard.remoting.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.TimerTask;

import com.fingard.FGBiz;
import com.fingard.dsp.tcpserver.RequestNIO;
import com.fingard.util.DateHelper;

public class TenantServerTimer  extends TimerTask {

	private HashMap<String,ArrayList<RequestNIO>> mapConnection;
	public TenantServerTimer(HashMap<String,ArrayList<RequestNIO>> pMapConnection){
		mapConnection = pMapConnection;
	}
	@Override
	public void run() {
		try{
			if(mapConnection!=null && mapConnection.size() > 0){
				for(String eachKey : mapConnection.keySet()){
					ArrayList<RequestNIO> aList = mapConnection.get(eachKey);
					if(aList!=null && aList.size() > 0){
						int count=aList.size();
						for(int i=count-1;i>=0;i--){
							RequestNIO remoting = aList.get(i);
							if(remoting!=null && remoting.lastRecDate!= null){
								double diffSecs = DateHelper.getDifferSecs(new Date(), remoting.lastRecDate);
								if(diffSecs>5){
									byte[] toWriteBytes = TenantServer.getBytes("<hb>hi</hb>",remoting.reqSocketReceiver.charsetName);
									synchronized(remoting.reqSocketReceiver){
										try {
											remoting.writeBytes(toWriteBytes);
										} catch (Throwable e) {
											remoting.reqSocketReceiver.isNeedCloseConn = true;
											FGBiz.limitMsg.exception("TenantServerTimer", e);
										}
									}
								}
							}
						}
					}
				}
			}
		}catch(Throwable e){
			FGBiz.limitMsg.exception("TenantServerTimer", e);
		}
	}

}
